package com.dream.web.validator;

import com.dream.dto.UserDTO;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 12-12-22
 * Time: 下午7:49
 */
public class UserValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return UserDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        UserDTO userDTO = (UserDTO) target;

        String username = userDTO.getUsername();
        if (!StringUtils.hasText(username)) {
            errors.rejectValue("username", null, "The username must not be empty!");
        }

        String password = userDTO.getPassword();
        if (!StringUtils.hasText(password)) {
            errors.rejectValue("password", null, "The password must not be empty!");
        }

        String reconfirmPassword = userDTO.getReconfirmPassword();
        if (!StringUtils.hasText(reconfirmPassword)) {
            errors.rejectValue("reconfirmPassword", null, "The re-confirm password must not be empty!");
        }

    }
}
